package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.JSONUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.vo.SelfViewpointForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.ChatManageBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.result.ChatContentResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.result.ChatListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.result.ChatListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.result.ChatMsgResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.result.ChatcontentData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.vo.ChatBatchDelForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.vo.ChatContentForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.operate.vo.ChatContentInfoForm;
import com.cloudbroker.bcs.platform.dav.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.dav.common.constant.CacheKeys;
import com.cloudbroker.bcs.platform.dav.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.dav.middle.entity.ChatMsg;
import com.cloudbroker.bcs.platform.dav.middle.entityexample.ChatBatchDelId;
import com.cloudbroker.bcs.platform.dav.middle.entityview.ChatContentView;
import com.cloudbroker.bcs.platform.dav.middle.entityview.ChatManageView;
import com.cloudbroker.bcs.platform.dav.middle.service.ChatMsgService;
import com.cloudbroker.bcs.platform.dav.middle.service.CustomerBrokerFollowService;
import com.cloudbroker.bcs.platform.dav.middle.service.CustomerBrokerRelService;
import com.cloudbroker.bcs.platform.dav.middle.service.TenantService;

/**
 * Created by lnq on 2017/1/3.
 */
@Service
public class ChatManageBizServiceImpl implements ChatManageBizService{
    @Resource
    private TenantService tenantService;
    @Resource
    private ChatMsgService chatMsgService;
    @Resource
    private CustomerBrokerFollowService customerBrokerFollowService;
    @Resource
    private CustomerBrokerRelService customerBrokerRelService;

    @Override
    public ChatListResult funcChatListByTenant(SelfViewpointForm form) {
        Integer page_no = ValueUtil.getInt(form.getPage_no(), 1);
        Integer page_count = ValueUtil.getInt(form.getPage_count(), ConfigValues.getRequestNum());
        List<Long> tenantIds =  tenantService.getTenantIdsByBranchNo(form.getBranch_no());
        if(tenantIds == null || tenantIds.size()<=0){
            return  VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.TENANT_NOT_EXIST_BY_BRANCH, ChatListResult.class);
        }
        Integer count = chatMsgService.getCountBytenant(tenantIds);
        List<ChatManageView> chatManageViews = chatMsgService.funcChatListByTenant((page_no - 1) * page_count,page_count,tenantIds);
        List<ChatListData> chatListDatas = new ArrayList<>();
        for(ChatManageView chatManageView : chatManageViews){
            ChatListData chatListData = new ChatListData();
            chatListData.setBroker_name(chatManageView.getBroker_name());
            chatListData.setSend_time(chatManageView.getSend_time());
            chatListData.setTopic(chatManageView.getTopic());
            chatListData.setTopic_id(chatManageView.getTopic_id());
            chatListData.setRoom_status(chatManageView.getRoom_status());
            chatListDatas.add(chatListData);
        };
        ChatListResult result = new ChatListResult();
        result.setData_list(chatListDatas);
        result.setPage_no(page_no);
        result.setTotal_item_num(count);
        return VOUtil.setSuccessResult(result);
    }

    @Override
    public ChatContentResult funcChatContent(ChatContentForm form) {
        String keyword = form.getKeyword();
        String topic_id = form.getTopic_id();
        Integer page_no = ValueUtil.getInt(form.getPage_no(), 1);
        Integer page_count = ValueUtil.getInt(form.getPage_count(), ConfigValues.getRequestNum());
        List<Long> tenantIds =  tenantService.getTenantIdsByBranchNo(form.getBranch_no());
        if(tenantIds == null || tenantIds.size()<=0){
            return  VOUtil.genErrorResult(HSBCPSErrorCodes.OperateErr.TENANT_NOT_EXIST_BY_BRANCH, ChatContentResult.class);
        }
        Integer count = chatMsgService.getChatContentCount(tenantIds,topic_id,keyword);
        List<ChatContentView> chatContentViews = chatMsgService.funcChatContent((page_no - 1) * page_count,page_count,tenantIds,topic_id,keyword);
        List<ChatcontentData> chatcontentDatas = new ArrayList<>();
        for(ChatContentView chatContentView : chatContentViews){
            ChatcontentData chatcontentData = new ChatcontentData();
            chatcontentData.setChat_msg_id(chatContentView.getChat_msg_id());
            chatcontentData.setMsg_content(chatContentView.getMsg_content());
            chatcontentData.setMsg_type(chatContentView.getMsg_type());
            chatcontentData.setTopic_id(chatContentView.getTopic_id());
            chatcontentData.setTopic(chatContentView.getTopic());
            chatcontentData.setPosition_str(chatContentView.getPosition_str());
            chatcontentData.setTenant_id(chatContentView.getTenant_id());
            chatcontentData.setBroker_id(chatContentView.getBroker_id());
            chatcontentDatas.add(chatcontentData);
        }
        ChatContentResult result = new ChatContentResult();
        result.setTotal_item_num(count);
        result.setPage_no(page_no);
        result.setPage_size(page_count);
        result.setData_list(chatcontentDatas);
        return VOUtil.setSuccessResult(result);
    }

    @Override
    public HSBCPSBackVO funcBatchDelChatContent(ChatBatchDelForm form) {
        String topicDelIds =  form.getTopic_del_ids();
        List<ChatBatchDelId> chatBatchDelIds = new ArrayList<>();
        JSONObject chatDeleteIdObject = JSON.parseObject(topicDelIds);
        JSONArray jsonArray = JSONUtil.toJsonArray(chatDeleteIdObject.get("chat_delete_id_list"));
        for (int i = 0; i < jsonArray.size(); i++) {
            ChatBatchDelId chatBatchDelId = new ChatBatchDelId();
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            chatBatchDelId.setTenant_id(jsonObj.getInteger("tenant_id"));
            chatBatchDelId.setChat_msg_id(jsonObj.getLong("chat_msg_id"));
            chatBatchDelIds.add(chatBatchDelId);
        }
        if(!chatBatchDelIds.isEmpty()){
            Integer count = chatMsgService.getChatCountByTopicId(chatBatchDelIds);
            if(!count.equals(chatBatchDelIds.size())){
                return VOUtil.genErrorResultVo(HSBCPSErrorCodes.OperateErr.DELETE_ID_ERR);
            }
            List<ChatMsg> chatMsgs = chatMsgService.getByChatMsgIds(chatBatchDelIds);
            for(ChatMsg cm : chatMsgs){
                String hisChat = CacheKeys.HIS_CHAT + cm.getBroker_id();
                String content = cm.getMsg_content();
                CacheHelper.zrem(hisChat, cm.getChat_msg_id(), content);
            }
            chatMsgService.deleteChatMsgByTopicId(chatBatchDelIds);
        }
        return VOUtil.genSuccessBackVo();
    }

    @Override
    public ChatMsgResult funcChatContentInfoGet(ChatContentInfoForm form) {
        String chatMsgId = form.getChatMsgId();
        ChatMsg chatMsg = chatMsgService.funcChatContentInfo(chatMsgId);
        ChatMsgResult result = new ChatMsgResult();
        result.setChat_msg_id(chatMsg.getChat_msg_id());
        result.setBroker_id(ValueUtil.getInt(chatMsg.getBroker_id()));
        result.setTopic(chatMsg.getTopic());
        result.setMsg_content(chatMsg.getMsg_content());
        result.setTenant_id(chatMsg.getTenant_id());
        return VOUtil.setSuccessResult(result);
    }
}
